import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import store from '@/stores/index';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: HomeView
    // },
    {
      path: '/login',
      meta: {
        layout: 'Login',
        title: '登录',
      },
      component: () => import('@/views/Login/Login.vue'),
      name: 'Login',
    },

    {
      path: '/dashboard',

      meta: {
        layout: 'defaultLayout',
        title: '',
      },
      name: 'dashboard',
      component: () => import('@/views/Dashboard/Dashboard.vue'),
    },
    {
      path: '/',
      redirect: '/dashboard',
    },
    {
      path: '/product',
      name: 'user',
      children: [
        {
          path: '/product/list',
          component: () => import('@/views/Product/ProductList.vue'),
          name: 'productList',
        },
      ],
      meta: {
        layout: 'defaultLayout',
        title: '',
      },
    },
    {
      path: '/table1',
      component: () => import('@/views/Table1.vue'),
      name: 'table1',
      meta: {
        layout: 'defaultLayout',
        title: '',
      },
    },
    {
      path: '/table2',
      component: () => import('@/views/Table2.vue'),
      name: 'table2',
      meta: {
        layout: 'defaultLayout',
        title: '',
      },
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断是否登录
  const uInfo = store.getters.userInfo;
  if (!uInfo.userName) {
    if (to.path === '/login') {
      next();
      return;
    }
    next('/login');
  }
  else {
    next();
  }
})
export default router
